<? 
	include('base.php');
	User::protect();
	$section = 'venues';
	include_class('venues');
	include_class('band_members');
	include_class('locations');
	$ve = Venue::get($_GET['id']);
	
	if (!db::isError($ve)) {
		switch($_GET['task']) {
			case 'update':
				$res = $ve->update($_POST);
				if (!db::isError($res)) {
					header('Location: venue_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'deactivate':
				$res = $ve->deactivate();
				if (!db::isError($res)) {
					header('Location: venue_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'activate':
				$res = $ve->activate();
				if (!db::isError($res)) {
					header('Location: venue_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'delete':
				$res = $ve->remove();
				if (!db::isError($res)) {
					header('Location: venues.php');
				}
				break;
		}
	}
	$editors = array('directions');
	$page_title = 'Edit Venue';
	include('layout/header.php');
?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="live.php">Live &#62;</a>
	<a href="venues.php">Venues &#62;</a>
	View/Edit Venue
</div>

<? 	if ($ve) { 

	if (db::isError($res)) {
		$res->outputList();
	}
	
	if ($ve->canEdit() && (($_GET['task'] == 'update') || ($_GET['task'] == 'edit'))) { 
	
		$name = $_POST ? $_POST['name'] : $ve->getName();
		$user_id = $_POST ? $_POST['user_id'] : $ve->getUserID();
		$address1 = $_POST ? $_POST['address1'] : $ve->getAddress1();
		$address2 = $_POST ? $_POST['address2'] : $ve->getAddress2();
		$city = $_POST ? $_POST['city'] : $ve->getCity();
		$stateProvince = $_POST ? $_POST['stateProvince'] : $ve->getStateProvince();
		$postalCode = $_POST ? $_POST['postalCode'] : $ve->getPostalCode();
		$country = $_POST ? $_POST['country'] : $ve->getCountry();
		$directions = $_POST ? $_POST['directions'] : $ve->getDirections();
		?>
		
		<h1>edit venue:</h1>
		<div class="inset">
		<form id="edit_entry" action="<?=$PHP_SELF?>?task=update&amp;id=<?=$_GET['id']?>" method="post">
		<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
		<? if (!$ve->isActive()) { ?>
		<tr>	
			<td colspan="2" class="fieldname">This venue is inactive.</td>
		</tr>
		<? } ?>
		<tr>
			<td width="50%"><strong>Name</strong><br/><input type="text" name="name" class="text" value="<?=$name?>"></td>
			<td><strong>Poster</strong> (* - inactive)<br/>
			<? if (User::isAdmin()) { ?>
					<select name="user_id">
						<?
						$ul = new UserList();
						$uArray = $ul->getAdminUsers();
						foreach ($uArray as $uo) { ?>
							<option class="admin-user" value="<?=$uo->getID()?>"<? if ($user_id == $uo->getID()) { ?> SELECTED<? } ?>><?=$uo->getFirstName()?><? if (!$uo->isActive()) { ?> *<? } ?></option>
						<? }
						
						$bmlist = new BandMemberList();
						$bmarray = $bmlist->get_band_members();
						foreach ($bmarray as $bm) {
							$uo = $bm->getUserObject();
						?>
							<option value="<?=$uo->getID()?>"<? if ($user_id == $uo->getID()) { ?> SELECTED<? } ?>><?=$uo->getFirstName()?><? if (!$uo->isActive()) { ?> *<? } ?></option>
						<? } ?>
					</select>
		
			<? } else { 
				$uo = $sh->getUserObject();
				echo($uo->getFirstName());
			} ?>					
			</td>
		</tr>
		<tr>
			<td width="50%"><strong>Address 1</strong><br/><input type="text" class="text"name="address1" value="<?=$address1?>"></td>
			<td width="50%"><strong>Address 2</strong><br/><input type="text" class="text" name="address2" value="<?=$address2?>">
			</td>
		</tr>
		</table>
		<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
		<tr>
			<td width="33%"><strong>City</strong><br/><input type="text" name="city" value="<?=$city?>"></td>
			<td width="34%"><strong>State/Province</strong><br/>
			<select id="stateProvince" name="stateProvince" onchange="toggleOther(this)">
			<? $sl = new StateList();
			$stateList = $sl->getStates();
			$selected = false;
			foreach ($stateList as $key => $sp) {
				echo("<option value=\"" . $key . "\"");
				if ($stateProvince == $key) {
					$selected = true;
					echo(" selected");
				}
				echo(">" . $sp . "</option>\r");
			} 
			echo("<option value=\"??\"");
			if (!$selected) { 
				echo(" selected");
			}
			echo(">** OTHER</option>");
			?>
			</select>
			</td>
			<td width="33%"><strong>If Other:</strong><br/><input <? if ($selected) { ?> disabled <? } ?> type="text" name="stateProvinceOther" value="<? if (!$selected) { ?><?=$stateProvince?><? } ?>"></td>
		</tr>
		<tr>
			<td><strong>Zip/Postal Code</strong><br/><input type="text" name="postalCode" value="<?=$postalCode?>"></td>
			<td><strong>Country</strong><br/>
			<select id="country" name="country">
			<? $sl = new CountryList();
			$countryList = $sl->getCountries();
			$selected = false;
			foreach ($countryList as $key => $co) {
				echo("<option value=\"" . $key . "\"");
				if ($country == $key) {
					$selected = true;
					echo(" selected");
				}
				echo(">" . $co . "</option>\r");
			} 
			?>
			</select>
			</td>
			<td>&nbsp;</td>
		</tr>
		</table>
		<div class="edit-form"><strong>Directions</strong><br/>
			<textarea name="directions" id="directions" class="mid"><?=$directions?></textarea>
		</div>
		<div class="edit-form">
			<input type="submit" value="Update Venue" class="button" name="submit">
			<input type="submit" value="Exit Without Updating" class="button" name="preview" onclick="location.href='<?=$PHP_SELF?>?id=<?=$ve->getID()?>'; return false">
		</div>
		</div>
		
		<script type="text/javascript">
			function toggleOther(sp) {
				if (sp.value == "??") { 
					document.forms[0].stateProvinceOther.disabled = false;
				} else {
					document.forms[0].stateProvinceOther.disabled = true;
					document.forms[0].stateProvinceOther.value = "";
				}
			}
		</script>


	<? } else { ?>

		<h1>view venue:</h1>
		<div class="inset">
		
		<h2><?
			echo($ve->getName());
		?></h2>
		
		<span class="small">
			Status: <strong><? if ($ve->isActive()) { ?>Active<? } else { ?>Inactive<? } ?></strong> |
			<? $uo = $ve->getUserObject() ?>
			Poster: <strong><?= $uo->getFirstName() ?></strong>
		</span>
			
		<br><br>
		<?=$ve->getAddress1()?><br/>
		<? 
		if ($ve->getAddress2()) {
			echo($ve->getAddress2() . '<br/>');
		}
		if ($ve->getCity()) {
			echo($ve->getCity());
		} else {
			echo("(no city)");
		}
		echo(", ");
		if ($ve->getStateProvince()) {
			echo($ve->getStateProvince());
		} else {
			echo("(no state/province)");
		}
		echo("&nbsp;&nbsp;");
		if ($ve->getPostalCode()) {
			echo($ve->getPostalCode());
		} else {
			echo("(no zip/postal)");
		}
		echo("<br/>");
		echo($ve->getCountryName());
		echo('<br/><br/>');
		if ($ve->getDirections()) {
			echo($ve->getDirections().'<br/><br/>');
		} ?>
		
		<form name="entry_status" method="get">
			<? if ($ve->canEdit()) { ?>
				<input type="submit" name="" value="Edit Venue" onclick="location.href='<?=$PHP_SELF?>?task=edit&id=<?=$_GET['id']?>';return false" class="button">
				<? if ($ve->isActive()) { ?><input type="submit" name="" value="Deactivate Venue" onclick="location.href='<?=$PHP_SELF?>?task=deactivate&id=<?=$_GET['id']?>';return false" class="button"><? } else { ?><input type="submit" name="" value="Activate Venue" onclick="location.href='<?=$PHP_SELF?>?task=activate&id=<?=$_GET['id']?>';return false" class="button"><? } ?>
			<? 
			
			}
			if (User::isAdmin()) { ?>
				<input type="submit" value="Remove Venue" name="" onclick="if (confirm('Are you sure you wish to delete this venue?\rWhenever possible, deactivate rather than delete.')) { location.href='<?=$PHP_SELF?>?task=delete&id=<?=$_GET['id']?>'};return false" class="button">
			<? } ?>
			<input type="submit" value="Return to Venue Listing" name="" onclick="location.href='venues.php'; return false" class="button">
		</form>
		
		</div>
	<? } ?>

<? } else { 
	$ve->outputDialog('Return to Venues','venues.php');
}

include('layout/footer.php'); ?>
